#include <stdio.h>
#include "head.h"

/*
 *功能:用户登录
 */
ER_TYPE do_login(int sockfd,MSG_T *msg,sqlite3 *db)
{
	int ret;
	char *errmsg;
#if 1 
	ret = _que_login(msg,db,&errmsg);
#endif 
	

	if(ret == ERROR){
	msg->errornum =ERROR ; 
		return ERROR;			    //用户登录失败
	}else{
		msg->errornum =OK ; 
	}
	printf("%s: username = %s\n",__func__,msg->user_info.name);
	printf("%s: password = %s\n",__func__,msg->user_info.password); 

	return OK;
}

/*
 *功能:查询信息 
 */
void do_query_1(int sockfd,MSG_T *msg,sqlite3 *db,char *err)
{
#if 1
	int i = 1;//查询列表计数器
	while(_que_root_person(msg,db,&err,i) != ERROR){
		server_send(sockfd,msg);
		i++;
	}
#endif
	msg->errornum = ERROR;
	server_send(sockfd,msg);
}
/*
 *功能:user 数据处理
 */
void handeler_user(int tempsockfd,sqlite3 *db)
{
	char *errmsg;
	MSG_T msg;
	int ret;
	int sockfd = tempsockfd;

	while(1){
		ret = server_recv(sockfd,&msg);
		if(ret == ERROR){
			continue;
		}
		else if(ret == 0){//断开链接
			break;
		}
		switch(msg.flag){
		case QUERY:
	printf("%s: username = %d\n",__func__,__LINE__);
#if 1
			if(_que_person(&msg,db,&errmsg) == ERROR){
				msg.errornum = ERROR;
				perror("fail to _que_person");
			}else{
			msg.errornum = OK;
			}
#endif
			break;
		case UPDATE:
	printf("%s: username = %d\n",__func__,__LINE__);
#if 1 
			if(_modify(&msg,db,&errmsg) == ERROR){
				msg.errornum = ERROR;
				perror("fail to _modify");
			}else{
			msg.errornum = OK;
			}
#endif 
			break;
		}

		server_send(sockfd,&msg);
	}

}

/**
 *功能:root 数据处理 
 * */
void handeler_root(int sockfdtmp,sqlite3 *db)
{
	char *errmsg;
	MSG_T msg;
	int ret;
	int sockfd = sockfdtmp;
	while(1){
		ret = server_recv(sockfd,&msg);
		if(ret == ERROR){
			continue;
		}
		else if(ret == 0){//断开链接
			break;
		}

		switch(msg.flag){
		case QUERY:
			do_query_1(sockfd,&msg,db,errmsg);
			break;
		case UPDATE:
	printf("%s: line = %d\n",__func__,__LINE__);
printf("1passward:%s  name:%s \n ",msg.user_info.password,msg.user_info.name);
#if 1 
			if(_modify(&msg,db,&errmsg) == ERROR){
				perror("fail to _modify");
				msg.errornum = ERROR;
			}else{
				msg.errornum = OK;
			}

#endif 
			server_send(sockfd,&msg);
			break;
		case DELETE:
	printf("%s: username = %d\n",__func__,__LINE__);
#if 1
			if(_delete(&msg,db,&errmsg) == ERROR){
				perror("fail to _delete");
				msg.errornum = ERROR;
			}else{
				msg.errornum = OK;
			}
#endif
			server_send(sockfd,&msg);
			break;
		case ADD:
	printf("%s: username = %d\n",__func__,__LINE__);
#if 1
			if(_add(&msg,db,&errmsg) == ERROR){
				perror("fail to _add"); 
				msg.errornum = ERROR;
			}else{
			msg.errornum = OK;
			}
#endif
			server_send(sockfd,&msg);
			break;
		default :break;
		}
	}

}
/*
 *功能:线程处理函数 i
 *参数:结构体 
 */
void *handeler_pthread(void *arg)
{
	MSG_T msg;
	sqlite3 *db;
	int ret; 
	char *errmsg;
	int sockfd = (int)*((int *)arg);
	printf("hander:socket::%d\n",sockfd);
#if 1
	if(_open_db(DATABASE,&db,&errmsg) == ERROR){
		perror("fail to _open_db");
	}
#endif
	while(1){
		ret = server_recv(sockfd,&msg);
		if(ret == ERROR){
				break;
				continue;
		}
		else if(ret == 0){//断开链接
			break;
		}
	
		switch(msg.usertype){
		case USER:
	printf("%s: username = %d\n",__func__,__LINE__);
	printf("%s: password = %d\n",__func__,__LINE__);
			
			if(do_login(sockfd,&msg,db) == OK){
				server_send(sockfd,&msg);
	printf("%s: password = %d\n",__func__,__LINE__);
				handeler_user(sockfd,db);
			}else{
				server_send(sockfd,&msg);

			}
			break;
		case ROOT:
			if(do_login(sockfd,&msg,db) == OK){
				server_send(sockfd,&msg);
			
				handeler_root(sockfd,db);
			}else{
				server_send(sockfd,&msg);

			}
			break;
		case REGISTER:	
#if 1
	printf("%s: username = %s, line=%d\n",__func__,msg.user_info.name,__LINE__);
	printf("%s: password = %s,line=%d\n",__func__,msg.user_info.password,__LINE__);

			if(_register(&msg,db,&errmsg) == ERROR){
				perror("fail to _register");
				msg.errornum = ERROR;
			}else{
			msg.errornum = OK;
			}
			server_send(sockfd,&msg);

#endif
			break;
		default :break;
		}
	
	}
#if 1
	if(_close_db(db) == ERROR){
		perror("fail to _close_db");
	}
#endif
}

/*
 *功能:创建线程
 *参数:套接字
 */
int thread_create(int sockfd)
{
	int ret;
	int tempsock = sockfd ;  
	pthread_t retid;
	printf("tempsock = %d , LIne =%d\n", tempsock,__LINE__);
	ret = pthread_create(&retid,NULL,handeler_pthread,&tempsock);
	printf("tempsock = %d , LIne =%d\n", tempsock,__LINE__);
	
	if(ret < 0){
		return ret;
	}
	sleep(1);
	pthread_detach(retid);
}


